import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:simple_app/common/api.dart';
import 'package:simple_app/common/model/friendSearch.dart';

class FriendSearchPage extends StatefulWidget {
  const FriendSearchPage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _FriendSearchPage();
}

class _FriendSearchPage extends State<FriendSearchPage> {
  @override
  void initState() {
    super.initState();
    // getList('');
  }

  @override
  void dispose() {
    super.dispose();
  }

  final searchTxt = TextEditingController();

  List<FriendSearch> list = [];
  int itemCount = 0;

  Future<void> getList(String keyword) async {
    if (keyword == '') {
      return;
    }
    var response = await Api().friendFind(keyword);
    if (response == null) {
      return;
    }
    if (response.statusCode != 200) {
      return;
    }
    if (response.statusCode == 200) {
      var jsonobj = jsonDecode(response.body);
      List<dynamic> list2 = jsonobj['list'];
      setState(() {
        list.clear();
        for (var i = 0; i < list2.length; i++) {
          list.add(FriendSearch.fromJson(list2[i]));
        }
        itemCount = list.length;
      });
    }
    return;
  }

  invite(BuildContext ctx, id) {
    Api().invite(id).then((response) {
      if (response == null) {
        issubmit = false;
        ScaffoldMessenger.of(ctx).showSnackBar(
          const SnackBar(content: Text('服务器错误')),
        );
        return;
      }
      if (response.statusCode != 200) {
        issubmit = false;
        ScaffoldMessenger.of(ctx).showSnackBar(
          const SnackBar(content: Text('服务器错误')),
        );
        return;
      }
      if (response.statusCode == 200) {
        var jsonobj = jsonDecode(response.body);
        if (jsonobj['status'] != 1) {
          issubmit = false;
          ScaffoldMessenger.of(ctx).showSnackBar(
            SnackBar(content: Text(jsonobj['info'])),
          );
          return;
        } else {
          issubmit = false;
          ScaffoldMessenger.of(ctx).showSnackBar(
            const SnackBar(content: Text('申请成功')),
          );
          return;
        }
      }
      issubmit = false;
    });
  }

  var issubmit = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('好友搜索'),
      ),
      body: Column(
        children: [
          Row(
            children: [
              Expanded(
                  child: TextFormField(
                controller: searchTxt,
              )),
              GestureDetector(
                child: Container(
                    padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
                    child: const Icon(Icons.search)),
                onTap: () {
                  getList(searchTxt.text);
                },
              ),
            ],
          ),
          Expanded(
              child: ListView.builder(
                  itemCount: itemCount,
                  itemBuilder: (ctx, index) {
                    return GestureDetector(
                      onTap: () {
                        if (issubmit) {
                          ScaffoldMessenger.of(ctx).showSnackBar(
                            const SnackBar(content: Text('有一个操作正在处理中...')),
                          );
                        }
                        if (!list[index].isFriend) {
                          issubmit = true;
                          invite(ctx, list[index].id);
                        }
                      },
                      child: ListTile(
                        title: Text(list[index].name),
                        subtitle: list[index].isFriend
                            ? const Text(
                                '已添加好友',
                                style: TextStyle(color: Color(0xff00ff00)),
                              )
                            : const Text('加好友'),
                      ),
                    );
                  }))
        ],
      ),
    );
  }
}
